<?xml version="1.0" encoding="UTF-8"?>
<!-- =================================================================
 |
 | Stylesheet list all devices in a library giving each device unique
 | number. Device with same characteristics has the same number.
 |
 | Generate uniqe number could have a prefix given as a parameter in
 | command line (saxon *.xml list-dev.xsl prefix=XXXX).
 |
 | This stylesheet use dirty workaround regarding "counter" variable.
 | Canging variable value is not supported by XSL or XPATH standard 1.0,
 | therefore xmlns:saxon namespace is used.
 |
 +   ================================================================= -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:saxon="http://icl.com/saxon"
                extension-element-prefixes="saxon"
                version="3.0">


<xsl:output omit-xml-declaration="no" indent="yes"/>


<xsl:strip-space elements="*"/>

<xsl:param name="prefix">ECAP</xsl:param>

<xsl:param name="startnum">0</xsl:param>

<xsl:variable name="counter" select="$startnum" saxon:assignable="yes"/>

<xsl:template match="node()|@*">
   <xsl:copy>
      <xsl:apply-templates select="node()|@*"/>
   </xsl:copy>
</xsl:template>


<!-- ================================================================
 |
 + ================================================================== -->
<xsl:template match="/">
   <xsl:call-template name="root.messages"/>
   <xsl:apply-templates/>
</xsl:template>

<!-- ================================================================
 |
 + ================================================================== -->
<xsl:template name="root.messages">
   <xsl:message>============================================================</xsl:message>
   <xsl:message> List of all devices in library                             </xsl:message>
   <xsl:message> Library prefix: <xsl:value-of select="$prefix" />           </xsl:message>
   <xsl:message>                                                            </xsl:message>
   <xsl:message> AUTHOR : martin.sveda@saabgroup.com                        </xsl:message>
   <xsl:message> USING  :<xsl:value-of select="system-property('xsl:vendor')"/></xsl:message>
   <xsl:message>============================================================</xsl:message>
   <xsl:message>                                                            </xsl:message>
</xsl:template>

<xsl:key name="kTechnology" match="technology" use="@hash"/>

<xsl:template match="/eagle">
   <hashcodes>
      <xsl:for-each select="//technology">
         <xsl:sort select="@hash" />
         <xsl:if test="generate-id() = generate-id(key('kTechnology', @hash)[1]) ">
            <saxon:assign name="counter" select="$counter+1"/>
            <xsl:element name="hash">
               <xsl:attribute name="mapping"><xsl:value-of select="@hash" /></xsl:attribute>
               <xsl:value-of select="$prefix"/>-<xsl:number value="$counter" format="000000"/>   
            </xsl:element>         
         </xsl:if>
      </xsl:for-each>
   </hashcodes>
</xsl:template>


</xsl:stylesheet> 
